# Copyright Vladimir Prus 2004.
# Copyright Noel Belcourt 2007.
# Copyright Nikita Kniazev 2020-2021.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt
# or copy at https://www.bfgroup.xyz/b2/LICENSE.txt)

import clang ;
import feature : feature ;
import os ;
import toolset ;
import toolset : flags ;
import gcc ;
import common ;
import errors ;
import generators ;

feature.extend-subfeature toolset clang : platform : darwin ;

toolset.inherit-generators clang-darwin
  <toolset>clang <toolset-clang:platform>darwin
  : gcc
  : gcc.mingw.link gcc.mingw.link.dll
  ;

generators.override clang-darwin.prebuilt : builtin.lib-generator ;
generators.override clang-darwin.prebuilt : builtin.prebuilt ;
generators.override clang-darwin.searched-lib-generator : searched-lib-generator ;

# Override default do-nothing generators.
generators.override clang-darwin.compile.c.pch   : pch.default-c-pch-generator   ;
generators.override clang-darwin.compile.c++.pch : pch.default-cpp-pch-generator ;

toolset.inherit-rules clang-darwin : gcc ;
toolset.inherit-flags clang-darwin : gcc
        : <inlining>full
          <architecture>x86/<address-model>32
          <architecture>x86/<address-model>64
          <lto>on/<lto-mode>full
          <lto>on/<lto-mode>fat
        ;

if [ MATCH (--debug-configuration) : [ modules.peek : ARGV ] ]
{
    .debug-configuration = true ;
}

# Initializes the clang-darwin toolset
#   version in optional
#   name (default clang++) is used to invoke the specified clang compiler
#   compile and link options allow you to specify addition command line options for each version
rule init ( version ? :  command * : options * )
{
    command = [ common.find-compiler clang-darwin : clang++ : $(version) : $(command)
                : /usr/bin /usr/local/bin ] ;
    local command-string = [ common.make-command-string $(command) ] ;
    if ! $(version) { # ?= operator does not short-circuit
    version ?= [ get-short-version $(command-string) ] ;
    }

    local condition = [ common.check-init-parameters clang
        : version $(version) ] ;

    common.handle-options clang-darwin : $(condition) : $(command) : $(options) ;
    clang.init-cxxstd-flags clang-darwin : $(condition) : $(version) ;

    # - Archive builder.
    local archiver = [ feature.get-values <archiver> : $(options) ] ;
    toolset.flags clang-darwin.archive .AR $(condition) : $(archiver[1]) ;
}

rule get-full-version ( command-string )
{
    import clang-linux ;
    return [ clang-linux.get-full-version $(command-string) ] ;
}

rule get-short-version ( command-string )
{
    import clang-linux ;
    return [ clang-linux.get-short-version $(command-string) ] ;
}

SPACE = " " ;

# Declare flags and action for compilation.

# For clang, 'on' and 'full' are identical
toolset.flags clang-darwin.compile OPTIONS <inlining>full : -Wno-inline ;

# LTO
toolset.flags clang-darwin.compile OPTIONS <lto>on/<lto-mode>thin : -flto=thin ;
toolset.flags clang-darwin.link OPTIONS <lto>on/<lto-mode>thin : -flto=thin ;

toolset.flags clang-darwin.compile OPTIONS <lto>on/<lto-mode>full : -flto=full ;
toolset.flags clang-darwin.link OPTIONS <lto>on/<lto-mode>full : -flto=full ;

# stdlib selection
toolset.flags clang-darwin.compile OPTIONS <stdlib>gnu <stdlib>gnu11 : -stdlib=libstdc++ ;
toolset.flags clang-darwin.link OPTIONS <stdlib>gnu <stdlib>gnu11 : -stdlib=libstdc++ ;

toolset.flags clang-darwin.compile OPTIONS <stdlib>libc++ : -stdlib=libc++ ;
toolset.flags clang-darwin.link OPTIONS <stdlib>libc++ : -stdlib=libc++ ;

rule compile.c ( targets * : sources * : properties * )
{
    DEPENDS $(<) : [ on $(<) return $(PCH_FILE) ] ;
}

rule compile.c++ ( targets * : sources * : properties * )
{
    DEPENDS $(<) : [ on $(<) return $(PCH_FILE) ] ;
}

.include-pch = "-include-pch " ;

actions compile.c bind PCH_FILE
{
    "$(CONFIG_COMMAND)" -x c $(OPTIONS) $(USER_OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" $(.include-pch)"$(PCH_FILE)" -c -o "$(<)" "$(>)"
}

actions compile.c++ bind PCH_FILE
{
    "$(CONFIG_COMMAND)" -x c++ $(OPTIONS) $(USER_OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" $(.include-pch)"$(PCH_FILE)" -c -o "$(<)" "$(>)"
}

# Declare actions for linking
rule link ( targets * : sources * : properties * )
{
  SPACE on $(targets) = " " ;
}

actions link bind LIBRARIES
{
    "$(CONFIG_COMMAND)" $(USER_OPTIONS) -L"$(LINKPATH)" -o "$(<)" "$(>)" "$(LIBRARIES)" $(START-GROUP) $(FINDLIBS-ST-PFX) -l$(FINDLIBS-ST) $(FINDLIBS-SA-PFX) -l$(FINDLIBS-SA) $(END-GROUP) $(OPTIONS)
}

actions link.dll bind LIBRARIES
{
    "$(CONFIG_COMMAND)" $(USER_OPTIONS) -L"$(LINKPATH)" -o "$(<)" -single_module -dynamiclib -install_name "@rpath/$(<[1]:D=)" "$(>)"  "$(LIBRARIES)" $(START-GROUP) $(FINDLIBS-ST-PFX) -l$(FINDLIBS-ST) $(FINDLIBS-SA-PFX) -l$(FINDLIBS-SA) $(END-GROUP) $(OPTIONS)
}
